package com.ruoyi.web.controller.contract;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ContractManageRelation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ContractManage;
import com.ruoyi.system.service.IContractManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-03-05
 */
@RestController
@RequestMapping("/contract/manage")
public class ContractManageController extends BaseController
{
    @Autowired
    private IContractManageService contractManageService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ContractManage contractManage)
    {
        startPage();
        List<ContractManage> list = contractManageService.selectContractManageList(contractManage);
        return getDataTable(list);
    }

    @GetMapping("/listProject")
    public AjaxResult listProject(ContractManage contractManage)
    {
        List<ContractManage> list = contractManageService.selectManageListByProjectId(contractManage);
        return success(list);
    }

    /**
     * 查询所有合同列表信息
     * 支持条件查询
     * @param contractManage
     * @return
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(ContractManage contractManage)
    {
        List<ContractManage> list = contractManageService.selectContractManageList(contractManage);
        return AjaxResult.success(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContractManage contractManage)
    {
        List<ContractManage> list = contractManageService.selectContractManageList(contractManage);
        ExcelUtil<ContractManage> util = new ExcelUtil<ContractManage>(ContractManage.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:manage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(contractManageService.selectContractManageById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContractManage contractManage)
    {
        return toAjax(contractManageService.insertContractManage(contractManage));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContractManage contractManage)
    {
        return toAjax(contractManageService.updateContractManage(contractManage));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(contractManageService.deleteContractManageByIds(ids));
    }

    /**
     * 删除关联合同
     * @param relation
     * @return
     */
    @PostMapping("/removeRelations")
    public AjaxResult removeRelations(@RequestBody ContractManageRelation relation)
    {
        return toAjax(contractManageService.deleteContractRelations(relation));
    }

    /**
     * 查询合同关联信息
     * @param contractManage
     * @return
     */
    @GetMapping("/getContractInfo")
    public AjaxResult getContractInfo(ContractManage contractManage) {
        Map<String, Object> stringObjectMap = contractManageService.selectContractRelationById(contractManage.getId());
        return AjaxResult.success(stringObjectMap);
    }

    /**
     * 查询合同总数
     * @param contractManage
     * @return
     */
    @GetMapping("/contractTotal")
    public AjaxResult selectContractTotal(ContractManage contractManage) {
        Map<String, Object> map = contractManageService.selectContractTotal(contractManage);
        return AjaxResult.success(map);
    }

    /**
     * 收入合同数量
     * @param contractManage
     * @return
     */
    @GetMapping("/incomeContractNum")
    public AjaxResult selectIncomeContractNum(ContractManage contractManage) {
        String dataDate = contractManage.getDataDate();
        if(StringUtils.isEmpty(dataDate)){
            contractManage.setDataDate(DateUtils.getDateDay());
        }
        contractManage.setDataEndDate(contractManage.getDataDate()+"-"+DateUtils.getDaysOfMonth(contractManage.getDataDate()));
        List<Map<String, Object>> maps = contractManageService.selectIncomeContractNum(contractManage);
        return AjaxResult.success(maps);
    }

    /**
     * 支出合同数量
     * @param contractManage
     * @return
     */
    @GetMapping("/expenditureContractNum")
    public AjaxResult selectExpenditureContractNum(ContractManage contractManage) {
        String dataDate = contractManage.getDataDate();
        if(StringUtils.isEmpty(dataDate)){
            contractManage.setDataDate(DateUtils.getDateDay());
        }
        contractManage.setDataEndDate(contractManage.getDataDate()+"-"+DateUtils.getDaysOfMonth(contractManage.getDataDate()));
        List<Map<String, Object>> maps = contractManageService.selectExpenditureContractNum(contractManage);
        return AjaxResult.success(maps);
    }

    /**
     * 收入合同金额
     * @param contractManage
     * @return
     */
    @GetMapping("/incomeContractAmount")
    public AjaxResult selectIncomeContractAmount(ContractManage contractManage) {
        String dataDate = contractManage.getDataDate();
        if(StringUtils.isEmpty(dataDate)){
            contractManage.setDataDate(DateUtils.getDateDay());
        }
        contractManage.setDataEndDate(contractManage.getDataDate()+"-"+DateUtils.getDaysOfMonth(contractManage.getDataDate()));
        List<Map<String, Object>> maps = contractManageService.selectIncomeContractAmount(contractManage);
        return AjaxResult.success(maps);
    }

    /**
     * 支出合同金额
     * @param contractManage
     * @return
     */
    @GetMapping("/expenditureContractAmount")
    public AjaxResult selectExpenditureContractAmount(ContractManage contractManage) {
        String dataDate = contractManage.getDataDate();
        if(StringUtils.isEmpty(dataDate)){
            contractManage.setDataDate(DateUtils.getDateDay());
        }
        contractManage.setDataEndDate(contractManage.getDataDate()+"-"+DateUtils.getDaysOfMonth(contractManage.getDataDate()));
        List<Map<String, Object>> maps = contractManageService.selectExpenditureContractAmount(contractManage);
        return AjaxResult.success(maps);
    }

    /**
     * 1 实际回款 2计划回款
     * @param contractManage
     * @return
     */
    @GetMapping("/collectionAndPlanData")
    public AjaxResult selectCollectionAndPlanData(ContractManage contractManage) {
        String dataDate = contractManage.getDataDate();
        if(StringUtils.isEmpty(dataDate)){
            contractManage.setDataDate(DateUtils.getDateDay());
        }
        contractManage.setDataEndDate(contractManage.getDataDate()+"-"+DateUtils.getDaysOfMonth(contractManage.getDataDate()));
        List<Map<String, Object>> maps = contractManageService.selectCollectionAndPlanData(contractManage);
        return AjaxResult.success(maps);
    }

    /**
     * 1 实际付款 2计划付款
     * @param contractManage
     * @return
     */
    @GetMapping("/paymentAndPlanData")
    public AjaxResult selectPaymentAndPlanData(ContractManage contractManage) {
        String dataDate = contractManage.getDataDate();
        if(StringUtils.isEmpty(dataDate)){
            contractManage.setDataDate(DateUtils.getDateDay());
        }
        contractManage.setDataEndDate(contractManage.getDataDate()+"-"+DateUtils.getDaysOfMonth(contractManage.getDataDate()));
        List<Map<String, Object>> maps = contractManageService.selectPaymentAndPlanData(contractManage);
        return AjaxResult.success(maps);
    }

    /**
     * 发票数据统计
     * @param contractManage
     * @return
     */
    @GetMapping("/invoiceData")
    public AjaxResult selectInvoiceData(ContractManage contractManage) {
        String dataDate = contractManage.getDataDate();
        if(StringUtils.isEmpty(dataDate)){
            contractManage.setDataDate(DateUtils.getDateDay());
        }
        contractManage.setDataEndDate(contractManage.getDataDate()+"-"+DateUtils.getDaysOfMonth(contractManage.getDataDate()));
        List<Map<String, Object>> maps = contractManageService.selectInvoiceData(contractManage);
        return AjaxResult.success(maps);
    }

}
